package com.dyz.leetcode._06_binarySearch;

public class _69_mySqrt {
    //ba暴露解法超时
    public int mySqrt1(int x) {
        int ans = -1;
        for(int k=0; k<=x; k++){
            if((long)k*k<=x){
                ans = k;
            }
        }
        return ans;
    }
    //二分
    public int mySqrt(int x) {
        int left = 0;
        int right = x;
        int ans = -1;
        while(left<=right){
            int k = left +(right-left)/2;
            if((long)k*k<=x){
                ans = k;
                left = k+1;
            }else{
                right = k-1;
            }
        }
        return ans;
    }
}
